<template>
	<view>
		<Tips />
		<uni-forms ref="registerRef" :modelValue="formData" :rules="register_rules">
			<uni-forms-item label="用户名" name="user_name">
				<uni-easyinput type="text" v-model="formData.user_name" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="pass_word">
				<uni-easyinput type="password" class="input" v-model="formData.pass_word" placeholder="请输入密码" />
			</uni-forms-item>
		</uni-forms>
		<button @click="submitRegister">注册</button>
		<button @click="submitLogin">登录</button>
	</view>
</template>

<script setup>
	import Tips from '@/components/Tips/Tips.vue'

	import {
		reactive,
		ref
	} from "vue";

	import {
		register_rules
	} from '../../../schema/register_schema.js'

	import {
		UserModel
	} from '@/service/model/user.js'

	const userModel = new UserModel()

	const registerRef = ref()
	const formData = reactive({
		user_name: 'wxlwxl',
		pass_word: 'wxlwxl666'
	})

	//提交注册
	const submitRegister = () => {
		registerRef.value.validate().then(async (res) => {
			const {
				data,
				success
			} = await userModel.postRegister(res)
			if (success) {
				uni.$emit('tips', data)
			}
		}).catch(err => {
		})
	}

	// 提交登录
	const submitLogin = () => {
		registerRef.value.validate().then(async (res) => {
			const {
				data,
				success
			} = await userModel.postLogin(res)
			if (success) {
				uni.setStorageSync('token', data)
				uni.navigateBack()
			}
		}).catch(err => {
		})
	}
</script>

<style lang="less">

</style>
